package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;
import com.stark.ve.core.SpeedMode;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityVideoSpeedBinding;
import flc.ast.utils.Content;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.ProgressConvertUtil;
import stark.common.basic.utils.TimeUtil;

public class VideoSpeedActivity extends BaseAc<ActivityVideoSpeedBinding> {

    public static String videoPath;
    private long videoLength;
    private float mCurSpeed = 1.0f;
    private float minSpeed;//最小速度
    private float maxSpeed;//最大速度

    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mHandler.postDelayed(this, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_speed;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoSpeedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        minSpeed = EditorHolder.editor().supportMinSpeed();
        maxSpeed = EditorHolder.editor().supportMaxSpeed();
        mDataBinding.sbSpeed.setMax(100);
        mDataBinding.sbSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mCurSpeed = ProgressConvertUtil.progress2value(progress, minSpeed, maxSpeed, 100);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        int curProgress = ProgressConvertUtil.value2Progress(mCurSpeed, minSpeed, maxSpeed, 100);
        mDataBinding.sbSpeed.setProgress(curProgress);
        mDataBinding.ivVideoSpeedSave.setOnClickListener(this);
        mDataBinding.ivPlayStart.setOnClickListener(this);
        mDataBinding.ivPlayFull.setOnClickListener(this);
        mDataBinding.llSpeed1.setOnClickListener(this);
        mDataBinding.llSpeed2.setOnClickListener(this);
        mDataBinding.llSpeed3.setOnClickListener(this);
        mDataBinding.llSpeed4.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlayStart://播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.abofang);
                    mDataBinding.videoView.pause();
                } else {
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.ivPlayFull://放大
                PlayActivity.videoUri = videoPath;
                startActivity(PlayActivity.class);
                break;
            case R.id.llSpeed1://0.5
                clearSelection();
                mDataBinding.llSpeed1.setBackgroundColor(Color.parseColor("#2F53FF"));
                mDataBinding.tvSpeed1.setTextColor(Color.parseColor("#FFFFFF"));
                mCurSpeed = 0.5f;
                int curProgress = ProgressConvertUtil.value2Progress(mCurSpeed, minSpeed, maxSpeed, 100);
                mDataBinding.sbSpeed.setProgress(curProgress);
                break;
            case R.id.llSpeed2://1.0
                clearSelection();
                mDataBinding.llSpeed2.setBackgroundColor(Color.parseColor("#2F53FF"));
                mDataBinding.tvSpeed2.setTextColor(Color.parseColor("#FFFFFF"));
                mCurSpeed = 1.0f;
                int curProgress2 = ProgressConvertUtil.value2Progress(mCurSpeed, minSpeed, maxSpeed, 100);
                mDataBinding.sbSpeed.setProgress(curProgress2);
                break;
            case R.id.llSpeed3://1.5
                clearSelection();
                mDataBinding.llSpeed3.setBackgroundColor(Color.parseColor("#2F53FF"));
                mDataBinding.tvSpeed3.setTextColor(Color.parseColor("#FFFFFF"));
                mCurSpeed = 1.5f;
                int curProgress3 = ProgressConvertUtil.value2Progress(mCurSpeed, minSpeed, maxSpeed, 100);
                mDataBinding.sbSpeed.setProgress(curProgress3);
                break;
            case R.id.llSpeed4://2.0
                clearSelection();
                mDataBinding.llSpeed4.setBackgroundColor(Color.parseColor("#2F53FF"));
                mDataBinding.tvSpeed4.setTextColor(Color.parseColor("#FFFFFF"));
                mCurSpeed = 2.0f;
                int curProgress4 = ProgressConvertUtil.value2Progress(mCurSpeed, minSpeed, maxSpeed, 100);
                mDataBinding.sbSpeed.setProgress(curProgress4);
                break;
            case R.id.ivVideoSpeedSave://保存
                save();
                break;
        }
    }

    @Override
    protected void initData() {
        videoLength = MediaUtil.getDuration(videoPath);
        //设置播放器
        setPlayer();
    }

    //设置播放器
    private void setPlayer() {
        mHandler = new Handler();
        mDataBinding.tvPlayLeft.setText("00:00");
        mDataBinding.tvPlayRight.setText(" / " + TimeUtil.getMmss(videoLength));
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.tvPlayLeft.setText("00:00");
                mDataBinding.ivPlayStart.setImageResource(R.drawable.abofang);
                mDataBinding.videoView.seekTo(1);
                stopTime();
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
        mDataBinding.videoView.start();
        startTime();
    }

    //变速
    private void save() {
        EditorHolder.editor().videoChangeSpeed(videoPath, mCurSpeed, SpeedMode.ALL, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.save_success));
                FileP2pUtil.copyPrivateVideoToPublic(mContext, outFilePath);
                startActivity(HomeActivity.class);
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.speed_def));
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.speed_ing) + progress + getString(R.string.unit_percent));
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTime();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlayStart.setImageResource(R.drawable.azant);
        mDataBinding.videoView.start();
        startTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    private void clearSelection() {
        mDataBinding.llSpeed1.setBackgroundColor(Color.parseColor("#F1F1F1"));
        mDataBinding.tvSpeed1.setTextColor(Color.parseColor("#000000"));
        mDataBinding.llSpeed2.setBackgroundColor(Color.parseColor("#F1F1F1"));
        mDataBinding.tvSpeed2.setTextColor(Color.parseColor("#000000"));
        mDataBinding.llSpeed3.setBackgroundColor(Color.parseColor("#F1F1F1"));
        mDataBinding.tvSpeed3.setTextColor(Color.parseColor("#000000"));
        mDataBinding.llSpeed4.setBackgroundColor(Color.parseColor("#F1F1F1"));
        mDataBinding.tvSpeed4.setTextColor(Color.parseColor("#000000"));
    }

}